package com.rub.prajna.av.controller

import com.rub.prajna.av.view.VideoView
import com.rub.prajna.core.video.VideoHandler
import com.rub.prajna.core.util.FileUtil
import javafx.beans.binding.Bindings
import javafx.collections.FXCollections
import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.geometry.Pos
import javafx.scene.control.Alert
import javafx.scene.control.ButtonType
import javafx.scene.layout.BorderPane.setAlignment
import javafx.scene.media.Media
import javafx.scene.media.MediaPlayer
import java.io.File
import java.net.URL
import java.util.*


/**
 * 1、?.表示当前对象如果为空则不执行，!!.表示当前对象如果为空也执行，然后会抛出空异常
 * 2、.mp4,.flv,.wmv,.avi,.mov,.dat,.asf,.rm,.rmvb,.ram,.mpg,.mpeg,.3gp,.m4v,.dvix,.dv,.mkv,.vob,.qt,.cpk,.fli,.flc,.mod,.ts,.webm,.m2ts
 * 3、文件处理: https://stackoverflow.com/questions/8323760/java-get-uri-from-filepath
 * 4@sample
 */
class VideoController : VideoView() {
    private var mediaPlayer: MediaPlayer? = null


    @FXML
    private fun onActionBtnOK(event: ActionEvent) {
        val inputFile = inputFile?.text
        if (inputFile == null || "" == inputFile) {
            Alert(Alert.AlertType.ERROR, "请选择视频文件", ButtonType.OK).show()
            return
        }

        val startHourVal = startHour?.value
        val startMinuteVal = startMinute?.value
        val startSecondVal = startSecond?.value

        if (startHourVal == null || "" == startHourVal) {
            Alert(Alert.AlertType.ERROR, "请选择开始时间", ButtonType.OK).show()
            return
        }
        if (startMinuteVal == null || "" == startMinuteVal) {
            Alert(Alert.AlertType.ERROR, "请选择开始时间", ButtonType.OK).show()
            return
        }
        if (startSecondVal == null || "" == startSecondVal) {
            Alert(Alert.AlertType.ERROR, "请选择开始时间", ButtonType.OK).show()
            return
        }

        val endHourVal = endHour?.value
        val endMinuteVal = endMinute?.value
        val endSecondVal = endSecond?.value

        if (endHourVal == null || "" == endHourVal) {
            Alert(Alert.AlertType.ERROR, "请选择结束时间", ButtonType.OK).show()
            return
        }
        if (endMinuteVal == null || "" == endMinuteVal) {
            Alert(Alert.AlertType.ERROR, "请选择结束时间", ButtonType.OK).show()
            return
        }
        if (endSecondVal == null || "" == endSecondVal) {
            Alert(Alert.AlertType.ERROR, "请选择结束时间", ButtonType.OK).show()
            return
        }

        val startTime = "$startHourVal:$startMinuteVal:$startSecondVal"
        val endTime = "$endHourVal:$endMinuteVal:$endSecondVal"

        val videoHandler = VideoHandler(inputFile).cutVideo(startTime, endTime)

        //成功设置输出路径
        if (videoHandler.getStatus()) {
            outputFile?.text = videoHandler.getOutPutFile()

            var outputFile = File(videoHandler.getOutPutFile()).toURI().toURL().toExternalForm()
            mediaPlayer = MediaPlayer(Media(outputFile))
            mediaView.mediaPlayer = mediaPlayer
            mediaPlayer?.isAutoPlay = true
            mediaPlayer?.play()

        }

    }

    @FXML
    private fun onActionReset() {
        inputFile?.text = ""
        //clear selection value
        startHour.selectionModel.clearSelection()
        startMinute.selectionModel.clearSelection()
        startSecond.selectionModel.clearSelection()
        endHour.selectionModel.clearSelection()
        endMinute.selectionModel.clearSelection()
        endSecond.selectionModel.clearSelection()
    }

    override fun initialize(location: URL?, resources: ResourceBundle?) {
        startHour.items = FXCollections.observableArrayList(timeLists(13))
        startMinute.items = FXCollections.observableArrayList(timeLists(60))
        startSecond.items = FXCollections.observableArrayList(timeLists(60))
        endHour.items = FXCollections.observableArrayList(timeLists(13))
        endMinute.items = FXCollections.observableArrayList(timeLists(60))
        endSecond.items = FXCollections.observableArrayList(timeLists(60))

        //默认值选择第一个值
        startHour.selectionModel.selectFirst()
        startMinute.selectionModel.selectFirst()
        startSecond.selectionModel.selectFirst()
        endHour.selectionModel.selectFirst()
        endMinute.selectionModel.selectFirst()
        endSecond.selectionModel.selectFirst()
        //禁止编辑
        outputFile.isEditable = false

    }


}


